<template>
  <div class="app-container">
    <!--工具栏-->
    <Search :crud="crud">
      <el-form-item label="类型">
        <el-select v-model="query.clubType" placeholder="请选择">
          <el-option
            v-for="item in dict.CLUB_TYPE"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </Search>
    <el-card>
      <div class="head-container">
        <crudOperation :permission="permission">
          <template slot="right">
            <el-button
              class="filter-item"
              size="mini"
              type="primary"
              icon="el-icon-plus"
              @click="handle(null, 'new')"
              >新建</el-button
            >
            <!-- <el-button class="filter-item" size="mini" type="primary" icon="el-icon-delete" @click="handle(null,'batchDelete')">批量删除</el-button> -->
          </template>
        </crudOperation>
      </div>
      <el-table
        ref="table"
        border
        v-loading="crud.loading"
        :data="crud.data"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
          fixed="left"
        />
        <el-table-column label="序号" type="index" width="50" align="center" />
        <el-table-column prop="clubType" label="类型" align="center">
          <template slot-scope="scope">
            <div>{{ showDictValue(scope.row.clubType, dict.CLUB_TYPE) }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="theme" label="主题" align="center" />
        <el-table-column prop="deadline" label="报名截止时间" align="center" />
        <el-table-column prop="createdByName" label="创建人" align="center" />
        <el-table-column
          prop="createdAtView"
          label="创建日期"
          width="200"
          align="center"
        />
        <el-table-column label="操作" align="center" width="220" fixed="right">
          <template slot-scope="scope">
            <el-button
              type="danger"
              size="mini"
              @click="handle(scope.row, 'delete')"
              >删除</el-button
            >
            <el-button
              type="primary"
              size="mini"
              @click="handle(scope.row, 'update')"
              >修改</el-button
            >
            <el-button
              type="success"
              size="mini"
              @click="handle(scope.row, 'query')"
              >查询</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!--分页组件-->
      <pagination />
    </el-card>
    <ClubDialog ref="clubDialog" @submitSuccess="submitSuccess"></ClubDialog>
  </div>
</template>

<script>
import crudJob from "@/api/system/job";
import CRUD, { presenter, header, crud } from "@crud/crud";
import crudOperation from "@crud/CRUD.operation";
import pagination from "@crud/Pagination";
import udOperation from "@crud/UD.operation";
import Search from "@/components/Search";
import product from "@/api/product/product";
import club from "@/api/club/index";
import ClubDialog from "@/views/club/clubDialog.vue";
import { showDictValue } from "@/utils";
export default {
  name: "BrandIntroductionManage",
  components: { crudOperation, pagination, udOperation, Search, ClubDialog },
  cruds() {
    return CRUD({
      title: "俱乐部列表",
      url: "club/list",
      requestMethod: "post",
      sort: ["jobSort,asc2", "id,desc"],
      // takingType:'3',
      otherParams: { takingType: "3" },
      crudMethod: { ...crudJob }
    });
  },
  // mixins: [presenter()],
  mixins: [presenter(), header(), crud()],
  // 数据字典
  dicts: ["CLUB_TYPE"],
  data() {
    return {
      permission: {
        add: ["admin", "job:add"],
        edit: ["admin", "job:edit"],
        del: ["admin", "job:del"]
      }
    };
  },
  methods: {
    showDictValue,
    // 改变状态
    changeEnabled(data, val) {
      product
        .tbDemoMoldTakingUpdateStatus({ id: data.id, isRelease: val })
        .then(() => {
          this.$notify({ message: "操作成功", type: "success" });
          this.crud.refresh();
        })
        .catch(err => {
          data.isRelease = !data.isRelease;
          if (data.isRelease == "1") {
            data.isRelease = "0";
          } else {
            data.isRelease = "1";
          }
        });
    },
    // 删除与修改事件
    handle(row, type) {
      if (type == "delete") {
        //删除
        this.deleteHandle(row);
      } else if (type == "update") {
        // 修改
        let forms = JSON.parse(JSON.stringify(row));
        this.$refs.clubDialog.form = {
          //forms
          id: forms.id,
          clubType: forms.clubType,
          theme: forms.theme,
          content: forms.content, // 发布标志，0-未发布，1-发布
          deadline: forms.deadline
        };
        this.$refs.clubDialog.isEdit = true;
        this.$refs.clubDialog.dialogVisible = true;
      } else if (type == "new") {
        // 新增
        this.$refs.clubDialog.form = {
          id: "",
          clubType: "",
          theme: "",
          content: "",
          deadline: ""
        };
        this.$refs.clubDialog.isEdit = false;
        this.$refs.clubDialog.dialogVisible = true;
      } else if (type == "batchDelete") {
        // 批量删除
        if (this.crud.selections.length == 0) {
          this.$notify({ title: "请选择需要删除的数据", type: "warning" });
          return;
        }
        const idList = [];
        for (let index = 0; index < this.crud.selections.length; index++) {
          const element = this.crud.selections[index];
          idList.push(element.id);
        }
        console.log(JSON.stringify(idList));
        this.deleteHandle(idList);
      } else if (type == "checkImg") {
        //查看图片
        let urlList = [];
        for (
          let index = 0;
          index < row.tbDemoMoldTakingFileList.length;
          index++
        ) {
          const element = row.tbDemoMoldTakingFileList[index];
          urlList.push(element.fileUrl);
        }
        this.$viewerApi({ images: urlList });
      } else if (type === "query") {
        this.$router.push({
          path: "/club/registration",
          query: {
            id: row.id
          }
        });
      }
    },
    // 删除
    deleteHandle(params) {
      this.$confirm("确定删除吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        club
          .clubDelete(params)
          .then(res => {
            this.$notify({ message: "操作成功", type: "success" });
            this.crud.refresh();
          })
          .catch(() => {});
      });
    },
    // 新建取模成功
    submitSuccess() {
      this.crud.refresh();
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-input-number .el-input__inner {
  text-align: left;
}
.item {
  margin-top: 10px;
  margin-right: 10px;
}
</style>
